package com.hww.acommon.test;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.hww.acommon.R;
import com.hww.common.MyAppThis;
import com.hww.common.dialog.CommonDialog;
import com.hww.common.util.DialogUtils;
import com.hww.common.util.ThreadUtils;
import com.hww.common.util.ToastUtils;
import com.hww.dialog.SweetAlertDialog;

import java.util.concurrent.ExecutorService;


/**
 * @author hww
 * @date 0017 2024/6/17
 * email: heww@zhongyuntech.cn
 * description:
 */
public class DialogActivity extends BaseActivity {

    private static final int REQUEST_CODE = 0x1001;
    private Context context;
    private static final ExecutorService singlePool = ThreadUtils.getSinglePool();

    @Override
    protected void initDate() {
        context = DialogActivity.this;
        updateLayout();

        setGONE();

        setBTText(binding.bt1, "只显示标题");
        setBTText(binding.bt2, "标题和内容");
        setBTText(binding.bt3, "异常样式");
        setBTText(binding.bt4, "警告样式");
        setBTText(binding.bt5, "成功完成样式");
        setBTText(binding.bt6, "自定义头像");
        setBTText(binding.bt7, "确定按钮与事件");
        setBTText(binding.bt8, "取消按钮与事件");
        setBTText(binding.bt9, "确认后切换对话框样式");
        setBTText(binding.bt10, "c_one");
        setBTText(binding.bt11, "c_two");
        setBTText(binding.bt12, "create_1");
        setBTText(binding.bt13, "create_2");

        binding.bt1.setOnClickListener((view) -> {
            System.out.println("base64--------------------");

            SweetAlertDialog dialog = getDialog(SweetAlertDialog.PROGRESS_TYPE);
            dialog.getProgressHelper().setBarColor(Color.parseColor("#A5DC86"));
            dialog.setTitleText("Loading...");
            dialog.setCancelable(true);
            dialog.setCanceledOnTouchOutside(true);
            dialog.show();

        });
        binding.bt2.setOnClickListener((view) -> {
            SweetAlertDialog dialog = getDialog(SweetAlertDialog.NORMAL_TYPE);
            dialog.setContentText("here's a message!");
            dialog.setTitleText("标题显示");
            dialog.setCancelable(true);
            dialog.show();
        });
        binding.bt3.setOnClickListener((view) -> {
            SweetAlertDialog dialog = getDialog(SweetAlertDialog.ERROR_TYPE);
            dialog.setContentText("here's a message!");
            dialog.setTitleText("标题显示");
            dialog.setCancelable(true);
            dialog.show();
        });

        binding.bt4.setOnClickListener((view) -> {
            SweetAlertDialog dialog = getDialog(SweetAlertDialog.WARNING_TYPE);
            dialog.setContentText("here's a message!");
            dialog.setTitleText("标题显示");
            dialog.setCancelable(true);
            dialog.show();
        });
        binding.bt5.setOnClickListener((view) -> {
            SweetAlertDialog dialog = getDialog(SweetAlertDialog.SUCCESS_TYPE);
            dialog.setContentText("here's a message!");
            dialog.setTitleText("标题显示");
            dialog.setCancelable(true);
            dialog.show();
        });
        binding.bt6.setOnClickListener((view) -> {
            System.out.println("file--------------------");
            SweetAlertDialog dialog = getDialog(SweetAlertDialog.CUSTOM_IMAGE_TYPE);
            dialog.setContentText("here's a message!");
            dialog.setTitleText("标题显示");
            dialog.setCustomImage(R.drawable.one);
            dialog.setCancelable(true);
            dialog.show();
        });
        binding.bt7.setOnClickListener((view) -> {
            SweetAlertDialog dialog = getDialog(SweetAlertDialog.SUCCESS_TYPE);
            dialog.setContentText("here's a message!")
                    .setTitleText("Are you sure?")
                    .setConfirmText("Yes")
                    .setConfirmClickListener(sweetAlertDialog -> {
                        mToast("点击了确定按钮");
                    })
                    .show();
        });
        binding.bt8.setOnClickListener((view) -> {
            SweetAlertDialog dialog = getDialog(SweetAlertDialog.SUCCESS_TYPE);
            dialog.setContentText("here's a message!");
            dialog.setTitleText("标题显示");
            dialog.setConfirmText("Yes");
            dialog.setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                @Override
                public void onClick(SweetAlertDialog sweetAlertDialog) {
                    sweetAlertDialog.cancel();
                    mToast("点击了确定按钮");
                }
            });
            dialog.setCancelText("cancel");
            dialog.setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
                @Override
                public void onClick(SweetAlertDialog sweetAlertDialog) {
                    sweetAlertDialog.dismissWithAnimation();
                    mToast("点击了取消按钮");
                }
            });
            dialog.show();
        });
        binding.bt9.setOnClickListener((view) -> {
            SweetAlertDialog dialog = getDialog(SweetAlertDialog.WARNING_TYPE);
            dialog.setContentText("here's a message!");
            dialog.setTitleText("标题显示");
            dialog.setTitleText("Yes");
            dialog.setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                @Override
                public void onClick(SweetAlertDialog sweetAlertDialog) {
                    sweetAlertDialog.setTitleText("切换后的").setContentText("......")
                            .setConfirmText("OK").setConfirmClickListener(null).changeAlertType(SweetAlertDialog.SUCCESS_TYPE);
                }
            });
            dialog.show();
        });
        binding.bt10.setOnClickListener((view) -> {
            CommonDialog commonDialog = new CommonDialog(DialogActivity.this);
            commonDialog.setTitle("标题")
                    .setMessage("test context")
                    .setPositive("确定")
                    .setNegative("取消")
                    .setOnClickBottomListener(new CommonDialog.OnClickBottomListener() {
                        @Override
                        public void onPositiveClick() {
                            ToastUtils.success(MyAppThis.getThis(), "点击了确定按钮").show();
                        }

                        @Override
                        public void onNegativeClick() {
                            ToastUtils.error(MyAppThis.getThis(), "点击了取消按钮").show();
                        }
                    }).show();

        });
        binding.bt11.setOnClickListener((view) -> {
            CommonDialog commonDialog = new CommonDialog(DialogActivity.this);
            commonDialog.setCancelable(true, false);
            commonDialog.setImageResId(R.drawable.one)
                    .setEditTxtVisibility(true)
                    .setPositive("确定")
                    .setNegative("取消")
                    .setOnClickBottomListener(new CommonDialog.OnClickBottomListener() {
                        @Override
                        public void onPositiveClick() {
                            String text = commonDialog.getEditText().getText().toString().trim();
                            ToastUtils.success(MyAppThis.getThis(), "点击了确定按钮, " + text).show();
                        }

                        @Override
                        public void onNegativeClick() {
                            ToastUtils.error(MyAppThis.getThis(), "点击了取消按钮").show();
                        }
                    }).showBottom();
        });
        binding.bt12.setOnClickListener((view) -> {
            View viewDialog = LayoutInflater.from(DialogActivity.this).inflate(R.layout.common_dialog_layout, null);
            Dialog dialog = DialogUtils.createDialogMarginScreen(DialogActivity.this, viewDialog, Gravity.TOP, true, false, 10f);
            dialog.show();
            ((TextView) viewDialog.findViewById(R.id.dialog_common_title)).setText("标题001");
            ((TextView) viewDialog.findViewById(R.id.dialog_common_message)).setText("显示内容001");
            Button bt1 = viewDialog.findViewById(R.id.dialog_common_positive);
            bt1.setText("确定保存");
            bt1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                    ToastUtils.success(MyAppThis.getThis(), "点击了确定按钮").show();
                }
            });

            Button bt2 = viewDialog.findViewById(R.id.dialog_common_negative);
            bt2.setText("取消");
            bt2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                    ToastUtils.error(MyAppThis.getThis(), "点击了取消按钮").show();
                }
            });
        });
        binding.bt13.setOnClickListener((view) -> {
            View viewDialog = DialogUtils.getViewFromResource(DialogActivity.this, R.layout.common_dialog_layout);
//            DialogUtils.createDialogFullScreen(DialogActivity.this, viewDialog, Gravity.TOP).show();
            DialogUtils.createDialogDefaultScreen(DialogActivity.this, viewDialog).show();
            ((TextView) viewDialog.findViewById(R.id.dialog_common_title)).setText("标题001");
            ((TextView) viewDialog.findViewById(R.id.dialog_common_message)).setText("显示内容001\n\n001");
            Button bt1 = viewDialog.findViewById(R.id.dialog_common_positive);
            bt1.setText("确定保存");
//            DialogUtils.createDialogFullScreen(DialogActivity.this, viewDialog, Gravity.TOP, true, false, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT).show();

        });
    }

    private void mToast(String msg) {
        ToastUtils.normal(MyAppThis.getThis(), msg).show();
    }

    /**
     * @param alertType {@linkplain SweetAlertDialog#PROGRESS_TYPE}
     * @return
     */
    private SweetAlertDialog getDialog(int alertType) {
        return new SweetAlertDialog(this, alertType);
    }


}
